// index.js
import {getHome} from "../../api/index.js"
import * as echarts from '../../ec-canvas/echarts.min';
var app = getApp()
import { getStorageItem, setStorageItem} from "../../utils/storage";
function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  var option = {
    color: ["#37A2DA"],
    title: {
      text: '',
      left: 'center'
    },
    grid: {
      containLabel: true,
      left:'-5%',
      top:'28%',
      bottom:'2%',
      right:'5%'
    },
    tooltip: {
      show: true,
      trigger: 'axis',
      
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: [0,6,12,18,24],
      axisTick: { //x轴刻度尺
        alignWithLabel: true,
        show: true,
        lineStyle: {
          type: 'dashed',
          color: '#D9D9D9'
        }
      },
      axisLine: {//x轴线条颜色
        lineStyle: {
          color: '#999'
        }
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'dashed',
          color: '#D9D9D9'
        }
      },
    },
    yAxis: {
      type: 'value',
      data: [0,6,12,18,24],
      axisTick: { //x轴刻度尺
        alignWithLabel: true,
        show: true,
        lineStyle: {
          type: 'dashed',
          color: '#D9D9D9'
        }
      },
      axisLine: {//x轴线条颜色
        lineStyle: {
          color: '#999'
        }
      },
      splitLine: {
        show: true,
        lineStyle: {
          type: 'dashed'
        }
      },
      show: false
    },
    series: [
      {
        data: [82,99,154, 232, 91, 134, 290, 83, 132],
        type: 'line',
        smooth: true,//面积图改成弧形状
        lineStyle: {
            width: 0,//外边线宽度
            color: ''//外边线颜色
        },
        showSymbol: false,//去除面积图节点圆
        areaStyle: {//区域填充渐变颜色
            color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [{
                    offset: 0, color: '#FC0985' // 0% 处的颜色
                }, {
                    offset: 0.9, color: '#870B1C' // 93.23% 处的颜色
                }],
                global: false
            }
        },
        // areaStyle: {
        //   normal: {
        //         color: new echarts.graphic.LinearGradient(1, 0, 0, 0, [{
        //             offset: 1,
        //             color: '#FC0985'
        //         },
        //         {
        //           offset: 0.5,
        //             color: '#870B1C'
        //         },
        //         {
        //             offset: 0,
        //             color: '#870B1C'
        //         }])
        //     }
        // },
      }
    ]
  };
  chart.setOption(option);
  return chart;
}
Page({
  data: {
    userInfo:{
      nickName:'测试',
      avatarUrl:'/images/nav/order-on.png'
    },
    consum_time:'2h3min',
    homeList: [
      {
        habit:'习惯',
        list:[
          {
            type:'吃早饭',
            days:30,
            daysList:[3,0],
            background:'#FF9900'
          },
          {
            type:'吃水果',
            days:60,
            daysList:[6,0],
            background: '#FC0985'
          },
          {
            type:'早起',
            days:19,
            daysList:[1,9],
            background: '#2E96FF'
          }
        ]
      },
      {
        habit:'纪念日',
        list:[
          {
            type:'过四级',
            days:22,
            daysList:[2,2],
            background: '#20A464'
          },
          {
            type:'过六级',
            days:31,
            daysList:[3,1],
            background: '#FF9900'
          },
        ]
      },
      {
        habit:'倒数日',
        list:[
          {
            type:'高考',
            days:345,
            daysList:[3,4,5],
            background: '#FE1114'
          },
          {
            type:'模拟考',
            days:162,
            daysList:[1,6,2],
            background: '#FE1114'
          },
        ]
      }
    ],
  },
  // 事件处理函数
  bindVTap(e) {
    const url = e.currentTarget.dataset.url
    if (url.includes("tab")) {
      // 是 tab 页面
      wx.$u.router({
        url: wx.$rConfig[url],
        type: "switchTab"
      })
    } else {
      wx.$u.router(wx.$rConfig[url])
    }
  },
  bindVTapParams(e) {
    const url = e.currentTarget.dataset.url
    wx.$u.router(wx.$rConfig[url], {
      a: 1,
      b: 2
    })
  },
  getMock() {
    // wx.$api.getHomeBanner({
    //   a: 1,
    //   b: 2
    // }).then(res => {
    //   console.log("wx.$api.getHomeBanner", res);
    // })
  },
  getMockHome() {
    getHome().then(res => {
      console.log("getMockHome:", res);
    })
  },
  onLoad() {
    this.setData({
      ec: {
        onInit: initChart
      }
    });
  },
  onShow: function () {
    let info = getStorageItem('userInfo')
    if(info!='') {
      this.setData({
        userInfo: getStorageItem('userInfo'),
      })
    }
  },
  getName(){
    let info = getStorageItem('userInfo')
    if(info==''){
      wx.getUserProfile({
        desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
        success: (res) => {
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          setStorageItem('userInfo',res.userInfo)
        }
      })
    }
  },
  addHabit(e){
    let str = e.currentTarget.dataset.id
    if(str=='习惯'){
      wx.$u.router(wx.$rConfig.habit)
    }
  },
  goNotice() {
    wx.$u.router(wx.$rConfig.systemMessage)
  },

})